/*!
 * TidyBlog 2.0
 * Login page - v1.0 (2013-10-06T09:53:57+0800)
 * https://code.google.com/p/tidyblog/ | Released under MIT license
 */
define(function(require, exports, module) { 'use strict';

require('/common/pagebase/1.0.x/');

var $ = require('dom/1.0.x/'),
	Captcha = require('/common/captcha/1.0.x/'),
	Validator = require('validator/1.0.x/'),
	util = require('/common/util/1.0.x/'),
	localStorage = window.localStorage;

// 启用验证码
new Captcha({
	wrapper: $('#captcha-img')
});

// 自动聚焦到用户名
var $userName = $('#userName');
var userName = localStorage ? localStorage.getItem('userName') : '';
if (userName) {
	$userName.val(userName);
	$('#password').focus();
} else {
	$userName.focus();
}

// 登录表单验证
new Validator({
	form: $('#login-form'),
	steps: [
		{ fields: "userName", message: "用户名不能为空" },
		{ fields: "password", message: "密码不能为空" },
		{ fields: "captcha", message: "验证码不能为空" }
	],
	events: {
		submiterror: util.onSubmitError,
		beforesubmit: function() {
			if (localStorage) {
				localStorage.setItem( 'userName', $userName.val().trim() );
			}
		}
	}
});

});